﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace GUI
{
    public partial class uc_DonVi : DevExpress.XtraEditors.XtraUserControl
    {
        #region khai bao bien

        DonVi_BLL dv_bll = new DonVi_BLL();
        DataTable dt;
        TextMessageBox box = new TextMessageBox();

        #endregion

        #region LoafForm

        public uc_DonVi()
        {
            InitializeComponent();
        }

        private void uc_DonVi_Load(object sender, EventArgs e)
        {
            Load_Data();
        }

        #endregion

        #region cac chức năng

        private void btn_ThemMoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmDonVi frm = new frmDonVi();
            if (frm.ShowDialog()==DialogResult.OK)
                Load_Data();
        }
       

        private void btn_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //if (grv_Data.RowCount > 0)
            //{
                //try
            if(grv_Data.FocusedRowHandle>=0)
                {
                    frmDonVi frm = new frmDonVi();
                    frm.maDonVi = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaDonVi).ToString();
                    if (frm.ShowDialog() == DialogResult.OK)
                        Load_Data();
                }
                //catch (Exception)
            else
                {
                    XtraMessageBox.Show(box.focuesFillter,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            //}
            //else
            //    XtraMessageBox.Show(box.NotData,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
        }

        private void btn_Xoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.RowCount > 0)
            {
                try
                {
                    DonVi_Public dv_public = new DonVi_Public();
                    string maDonVi = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaDonVi).ToString();
                    string tenDonVi = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colTenDonVi).ToString();
                    dv_public.MaDonVi = maDonVi;
                    dt = dv_bll.TaoBang(box.sqlCheckExist("DM_Lop", "MaDonVi", maDonVi));
                    if (dt.Rows.Count > 0)
                    {
                        XtraMessageBox.Show(box.NotDelete(maDonVi), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        if (XtraMessageBox.Show(box.Delete_Confirm(maDonVi, tenDonVi), box.Caption_Confirm, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                        {
                            try
                            {
                                dv_bll.Delete(dv_public);
                                Load_Data();
                            }
                            catch (Exception)
                            {
                                XtraMessageBox.Show(box.DeleteFail, box.Caption_Fail, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    XtraMessageBox.Show(box.focuesFillter,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                }

            }
            else
                XtraMessageBox.Show(box.NotData,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);



        }
        #endregion

        #region hàm viết thêm

        private void Load_Data()
        {
            grd_Data.DataSource = dv_bll.LayDuLieu();
            //grv_Data.BestFitColumns();
        }

        #endregion
    }
}
